<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="赛事名称" prop="competitionName">
                    <el-input class="w-[280px]" v-model="queryParams.competitionName" />
                </el-form-item>
                <el-form-item label="社团名称" prop="teamName">
                    <el-input class="w-[280px]" v-model="queryParams.teamName" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select
                        v-model="queryParams.status"
                        class="w-[280px]"
                        clearable
                    >
                        <el-option label="待审核" value="0" />
                        <el-option label="通过" value="0" />
                        <el-option label="未通过" value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['apply:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table
                class="mt-4"
                size="large"
                v-loading="pager.loading"
                :data="pager.lists"
            >
                <el-table-column label="赛事名称" prop="competitionName" min-width="100" />
                <el-table-column label="社团名称" prop="teamName" min-width="100" />
                <el-table-column label="状态" min-width="80">
                  <template #default="{ row }">
                    <el-tag v-if="row.status == 0">待审核</el-tag>
                    <el-tag type="success" v-else-if="row.status == 1">通过</el-tag>
                    <el-tag type="danger" v-else>未通过</el-tag>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['competition.lists/examine']"
                            v-if="row.status == 0"
                            type="primary"
                            link
                            @click="handleExamine(row)"
                        >
                          审核
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>
<script lang="ts" setup name="apply">
import {applyDelete, applyLists} from '@/api/apply'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {applyExamine} from "@/api/competition/competition";
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    competitionName: '',
    teamName: '',
    status: '',
    createId: '',
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: applyLists,
    params: queryParams
})



const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await applyDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

const handleExamine = async (obj: any) => {
  let status = obj.status;
  await feedback.confirm('审核【'+obj.teamName+'】提交赛事【'+obj.competitionName+'】是否通过？').then(() => {
    status = 1
  }).catch(() => {
    status = 2
  })
  await applyExamine({ id:obj.id,status:status })
  getLists()
}

getLists()
</script>
